package cn.broadmax.boot.base.message;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * @author jeff
 * @date 2020/6/14
 */
@Data
@ApiModel(value="接口返回对象", description="接口返回对象")
public class SResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "成功标志")
    private boolean success = true;

    @ApiModelProperty(value = "返回代码")
    private Integer code = 0;

    @ApiModelProperty(value = "返回处理消息")
    private String message = "操作成功！";

    @ApiModelProperty(value = "返回数据对象")
    private T result;

    @ApiModelProperty(value = "时间戳")
    private long timestamp = System.currentTimeMillis();

    public SResult() {
    }

    public SResult(int code, String message, T result){
        this.code = code;
        this.message = message;
        this.result = result;
        int seriesCode = code / 100;
        this.success = seriesCode == 2;
    }


    public static <T> SResult<T> of(int code, String message, T result){
        return new SResult<>(code, message, result);
    }

    public static SResult<Object> ok(){
        return ok("");
    }

    public static SResult<Object> ok(String message){
        return ok(message, "");
    }

    public static <T> SResult<T> ok(String message, T result){
        return of(HttpStatus.OK.value(), message, result);
    }

    public static SResult<Object> warn(String message){
        return warn(message, "");
    }

    public static <T> SResult<T> warn(String message, T result){
        return of(HttpStatus.BAD_REQUEST.value(), message, result);
    }

    public static SResult<Object> error(String message){
        return error(message, "");
    }

    public static <T> SResult<T> error(String message, T result){
        return of(HttpStatus.INTERNAL_SERVER_ERROR.value(), message, result);
    }

    public static SResult<Object> unauth(String message){
        return unauth(message, "");
    }

    public static SResult<Object> unauth(String message, String result){
        return of(HttpStatus.UNAUTHORIZED.value(), message, result);
    }

}
